let goodId=0
export default {
  namespace: 'goods',
  state: {content:[],curPage:0,updId:-1},
  reducers: {
    add(state,action){
      // console.log(action)
      return Object.assign({},state,{
        content:[
          {
            id:goodId++,
            ...action.good
          },...state.content
        ]
      })
    },
    page(state,action){
      return Object.assign({},state,{curPage:action.curPage})
    },
del(state,{id}){
      return Object.assign({},state,{
        content:state.content.filter(goods=>{
          return goods.id!=id
        })
      })
},
    // toupd(state,{id}){
    //   return Object.assign({},state,{updId:id})
    // },
    toupd(state, action) {
      let goods = {...state}
      goods.updId = action.id
      return goods
    },
    upd(state, action) {
      let content = state.content.map(goods => {
        if (goods.id == action.goods.id) {
          return Object.assign({}, goods, action.goods)
        } else {
          return goods
        }
      })
      return Object.assign({}, state, {content, updId: -1})
    }
  },
  effects: {},
  subscriptions: {},
};
